package com.zxmzy.scaffold.core.base

import android.content.Context
import android.content.res.Configuration
import android.os.Bundle
import android.view.ContextThemeWrapper
import android.view.LayoutInflater
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import androidx.viewbinding.ViewBinding
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.zxmzy.scaffold.core.R
import com.zxmzy.scaffold.core.ext.saveAs
import com.zxmzy.scaffold.core.ext.saveAsUnChecked
import com.zxmzy.scaffold.core.utils.LanguageUtil
import com.zxmzy.scaffold.core.widget.slideback.SlideBack
import java.lang.reflect.ParameterizedType

/**
 * @author zhangxiaoming
 * @date 2023/7/25
 * @description Activity基类
 */
abstract class BaseActivity<VB : ViewBinding> : AppCompatActivity() {
    protected lateinit var mBinding: VB
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentLayout()
        initImmersionBar()
        initView(savedInstanceState)
        initData()
        if (canSwipeBack()) {
            SlideBack.create().attachToActivity(this)
        }
    }

    override fun attachBaseContext(newBase: Context) {
        if (shouldSupportMultiLanguage()) {
            val context = LanguageUtil.attachBaseContext(newBase)
            val configuration = context.resources.configuration
            // 此处的ContextThemeWrapper是androidx.appcompat.view包下的
            // 你也可以使用android.view.ContextThemeWrapper，但是使用该对象最低只兼容到API 17
            // 所以使用 androidx.appcompat.view.ContextThemeWrapper省心
            val wrappedContext = object : ContextThemeWrapper(context, R.style.AppTheme) {
                override fun applyOverrideConfiguration(overrideConfiguration: Configuration?) {
                    overrideConfiguration?.setTo(configuration)
                    super.applyOverrideConfiguration(overrideConfiguration)
                }
            }
            super.attachBaseContext(wrappedContext)
        } else {
            super.attachBaseContext(newBase)
        }
    }


    protected open fun shouldSupportMultiLanguage(): Boolean = true

    private fun setContentLayout() {
        val type = javaClass.genericSuperclass
        val vbClass: Class<VB> = type!!.saveAs<ParameterizedType>().actualTypeArguments[0].saveAs()
        val method = vbClass.getDeclaredMethod("inflate", LayoutInflater::class.java)
        mBinding = method.invoke(this, layoutInflater)!!.saveAsUnChecked()
        setContentView(mBinding.root)

    }

    /**
     * 初始化布局
     */
    abstract fun initView(savedInstanceState: Bundle?)

    /**
     * 初始化数据
     */
    open fun initData() {}
    protected open fun fullScreen(): Boolean {
        return true
    }

    open fun initImmersionBar() {
        if (!fullScreen()) {
            ImmersionBar.with(this)
                .statusBarView(R.id.statusBarView)
                .statusBarDarkFont(true)
                .transparentBar()
                .keyboardEnable(true)
                .keyboardMode(keyboardMode())
                .hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR)
                .init()

        } else {
            ImmersionBar.with(this)
                .fullScreen(true)
                .keyboardEnable(true)
                .keyboardMode(keyboardMode())
                .hideBar(BarHide.FLAG_HIDE_BAR)
                .init()
        }
    }

    @Suppress("DEPRECATION")
    open fun keyboardMode(): Int {
        return WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN or WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
    }

    protected open fun canSwipeBack() = true
}